import pygame
from pygame.locals import *
import sys
import time
import random
import _thread
pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("屈原吃粽子")
quyuan = pygame.image.load("quyuan.png").convert_alpha()
zongzi = pygame.image.load("zongzi.png").convert_alpha()
quyuan = pygame.transform.scale(quyuan, (50, 50))
zongzi = pygame.transform.scale(zongzi, (50, 50))
pygame.display.set_icon(zongzi)
learned = False
gameover = False
miluo = [
    pygame.Rect(0, 0, 500, 50),
    pygame.Rect(0, 450, 500, 50),
    pygame.Rect(0, 0, 50, 500),
    pygame.Rect(450, 0, 50, 500),
]
score = 0
qy_dir = (0, 0)
qy_pos = (7, 7)
zz_pos = (3, 3)
qy_wtime = 0.5
qy_rect = quyuan.get_rect()
qy_rect.left = 350
qy_rect.top = 350
font = lambda s, w, c: pygame.font.Font("AlibabaFont.otf", s).render(w, True, c)
def update_quyuan():
    global qy_pos, zz_pos, qy_rect, qy_wtime, score, gameover
    while True:
        time.sleep(qy_wtime)
        qy_pos = (qy_pos[0]+qy_dir[0], qy_pos[1]+qy_dir[1])
        qy_rect.left = qy_pos[0] * 50
        qy_rect.top = qy_pos[1] * 50
        if qy_pos == zz_pos:
            score += 1
            zz_pos = (random.randint(1, 8), random.randint(1, 8))
            qy_wtime = qy_wtime * 0.9
_thread.start_new_thread(update_quyuan, ())
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_UP:
                qy_dir = (0, -1)
                learned = True
            elif event.key == K_DOWN:
                qy_dir = (0, 1)
                learned = True
            elif event.key == K_LEFT:
                qy_dir = (-1, 0)
                learned = True
            elif event.key == K_RIGHT:
                qy_dir = (1, 0)
                learned = True
    screen.fill("lightgray")
    screen.blit(quyuan, qy_rect)
    screen.blit(zongzi, (zz_pos[0]*50, zz_pos[1]*50))
    for i in miluo:
        pygame.draw.rect(screen, (0, 192, 255), i)
        if i.colliderect(qy_rect):
            gameover = True
    mil = font(20, "汨罗江", "darkblue")
    milr = mil.get_rect()
    milr.top = 10
    milr.centerx = 250
    screen.blit(mil, milr)
    if gameover:
        screen.fill("lightgray")
        go = font(48, "GAME OVER", "green")
        tip = font(24, "重启游戏再次游玩", "blue")
        gor = go.get_rect()
        tipr = tip.get_rect()
        gor.top = 150
        gor.centerx = 250
        tipr.top = 250
        tipr.centerx = 250
        screen.blit(go, gor)
        screen.blit(tip, tipr)
    if not learned:
        tit = font(48, "屈原吃粽子", "darkcyan")
        tip = font(24, "上下左右键控制方向", "blue")
        titr = tit.get_rect()
        tipr = tip.get_rect()
        titr.top = 150
        titr.centerx = 250
        tipr.top = 250
        tipr.centerx = 250
        screen.blit(tit, titr)
        screen.blit(tip, tipr)
    sc_surf = font(20, "Score: {}".format(score), "black")
    screen.blit(sc_surf, (50, 450))
    pygame.display.flip()